package com.liuqi.business.service.impl;


import com.liuqi.base.BaseMapper;
import com.liuqi.base.BaseServiceImpl;
import com.liuqi.business.constant.KeyConstant;
import com.liuqi.business.enums.ProtocolEnum;
import com.liuqi.business.enums.SwitchEnum;
import com.liuqi.business.mapper.CurrencyRechargeMapper;
import com.liuqi.business.model.CurrencyExtractModelDto;
import com.liuqi.business.model.CurrencyRechargeModel;
import com.liuqi.business.model.CurrencyRechargeModelDto;
import com.liuqi.business.service.CurrencyRechargeService;
import com.liuqi.business.service.CurrencyService;
import com.liuqi.redis.RedisRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service
@Transactional(readOnly = true)
public class CurrencyRechargeServiceImpl extends BaseServiceImpl<CurrencyRechargeModel,CurrencyRechargeModelDto> implements CurrencyRechargeService{

	@Autowired
	private CurrencyRechargeMapper currencyRechargeMapper;
	@Autowired
	private RedisRepository redisRepository;
	@Autowired
	private CurrencyService currencyService;

	@Override
	public BaseMapper<CurrencyRechargeModel,CurrencyRechargeModelDto> getBaseMapper() {
		return this.currencyRechargeMapper;
	}

	@Override
	protected void doMode(CurrencyRechargeModelDto dto) {
		super.doMode(dto);
		dto.setCurrencyName(currencyService.getNameById(dto.getCurrencyId()));
		dto.setProtocolStr(ProtocolEnum.getName(dto.getProtocol()));
	}

	@Override
	public CurrencyRechargeModelDto getById(Long id) {
		String key= KeyConstant.KEY_RECHARGE_ID+id;
		CurrencyRechargeModelDto recharge = redisRepository.getModel(key);
		if (recharge == null) {
			recharge=currencyRechargeMapper.getById(id);
			if(recharge!=null){
				redisRepository.set(key, recharge, 1L, TimeUnit.DAYS);
			}
		}
		return recharge;
	}

	@Override
	public List<CurrencyRechargeModelDto> getRechargeList() {
		String key= KeyConstant.KEY_RECHARGE_LIST;
		List<CurrencyRechargeModelDto> list=redisRepository.getModel(key);
		if(list==null){
			list=this.queryListByDto(new CurrencyRechargeModelDto(),true);
			if(list!=null){
				redisRepository.set(key,list,10L, TimeUnit.DAYS);
			}
		}
		return list;
	}


	@Override
	public List<CurrencyRechargeModelDto> getOnRechargeList() {
		List<CurrencyRechargeModelDto> list=this.getRechargeList();
		return list.stream().filter(t-> SwitchEnum.isOn(t.getRechargeSwitch())).collect(Collectors.toList());
	}

	@Override
	public CurrencyRechargeModelDto getByCurrencyAndProtocol(Long currencyId, Integer protocol) {
		return currencyRechargeMapper.getByCurrencyAndProtocol(currencyId,protocol);
	}

	@Override
	public void cleanCacheByModel(CurrencyRechargeModel currencyRechargeModel) {
		super.cleanCacheByModel(currencyRechargeModel);
		String key= KeyConstant.KEY_RECHARGE_ID+currencyRechargeModel.getId();
		redisRepository.del(key,KeyConstant.KEY_RECHARGE_LIST);
	}

	@Override
	public void cleanAllCache() {
		super.cleanAllCache();
		String key= KeyConstant.KEY_RECHARGE_LIST;
		redisRepository.del(key);
	}
}
